#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 crane <crane@his-pc>
#
# Distributed under terms of the MIT license.

"""

"""


class Solution:
    """
    @param str: A string
    @return: An integer
    """
    POSTIVIE_MAX_STR = "2147483647"
    POSTIVIE_MAX     = 2147483647
    EXCEED_DIVID_10  = POSTIVIE_MAX // 10

    NEGATIVE_MAX_STR = "2147483648"
    NEGATIVE_MAX     = -2147483648

    def atoi(self, s):
        # get sign
        s = s.strip()
        if s == "":
            return 0

        sign = 1
        if s[0] == '-':
            sign = -1
            s = s[1:]
        elif s[0] == '+':
            sigin = 1
            s = s[1:]

        # trim integer part
        integer_str = s.split('.')[0]

        # exceed max?
        # if sign == 1:
        #     max_int = self.POSTIVIE_MAX
        #     max_str = self.POSTIVIE_MAX_STR
        # else:
        #     max_int = self.NEGATIVE_MAX
        #     max_str = self.NEGATIVE_MAX_STR

        # if self.cmp_str_integer(integer_str, max_str):
        #     print('here')
        #     value = max_int
        # else:
        #     value = self.str_to_int(integer_str) * sign
        value = self.str_to_int(integer_str, sign)

        return value


    # def cmp_str_integer(self, a, b):
    #     # TODO: 其它语言自己实现字符串比较
    #     # params: a 和 b是字符串形式的int
    #     print(a)

    #     if len(a) > len(b):
    #         return True
    #     if len(a) < len(b):
    #         return False

    #     # len(a) == len(b)
    #     # for a_i, b_i in zip(a, b):
    #     #     digit_a = ord(a_i) - ord('0')
    #     #     digit_b = ord(b_i) - ord('0')
    #     #     # if digit_a > 9 or digit_a < 0:
    #     #     #     return False
    #     #     if digit_a > 9:
    #     #         return False
    #     #     elif digit_a > digit_b:
    #     #         return True
    #     #     elif digit_a < digit_b:
    #     #         return False
    #     for a_i in a:
    #         digit_a = ord(a_i) - ord('0')
    #         if digit_a > 9 or digit_a < 0:
    #             return False

    #     return a > b

    def str_to_int(self, s, sign):
        # try:
        #     value = int(s)
        # except Exception as e:
        #     print('hit')
        #     value = 0

        # "151_SOME" return 151
        sum = 0

        for i in s:
            digit = ord(i) - ord('0')
            if digit > 9 or digit < 0:
                break

            if sum > self.EXCEED_DIVID_10:
                return self.max(sign)
            if sum == self.EXCEED_DIVID_10:
                return self.bound(sum, digit, sign)

            sum = sum * 10 + digit

        return sum * sign

    def bound(self, shiwei, digit, sign):
        max_digit = self.max_digit(sign)
        if digit >= max_digit:
            return self.max(sign)
        else:
            return (shiwei * 10 + digit) * sign

        # if sign == 1:
        #     if digit >= 7:
        #         return self.max(sign)
        #     else:
        #         return shiwei * 10 + digit
        # else:
        #     assert sign == -1
        #     if digit >= 8:
        #         return self.max(sign)
        #     else:
        #         return (shiwei * 10 + digit) * sign

    def max(self, sign):
        if sign == 1:
            return self.POSTIVIE_MAX
        else:
            return self.NEGATIVE_MAX

    def max_digit(self, sign):
        if sign == 1:
            return 7
        else:
            return 8


def main():
    print("start main")
    s = Solution()

    # ret = s.atoi('-1234')
    # print(ret)

    # ret = s.atoi('-0')
    # print(ret)

    # ret = s.atoi('2147483641234.1234')
    # print(ret)

    # ret = s.atoi('-214748364823')
    # print(ret)

    # ret = s.atoi(' +15+4')
    # ret = s.atoi('    52lintcode   ')
    ret = s.atoi('        -2147483649')
    # ret = s.atoi('  ')
    print(ret)

if __name__ == "__main__":
    main()
